/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.support.interceptor;

import java.util.Locale;
import javax.swing.text.JTextComponent;
import org.jdesktop.xswingx.PromptSupport;
import org.springframework.binding.form.FormModel;
import org.springframework.context.MessageSource;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.richclient.form.builder.FormComponentInterceptor;
import org.springframework.richclient.text.TextComponentInterceptor;
import org.springframework.util.StringUtils;
import org.springframework.richclient.form.builder.FormComponentInterceptorFactory;

/**
 *
 * @author renan_luz
 */
public class PromptTextFieldFormComponentInterceptor extends TextComponentInterceptor {

    private MessageSource messageSource;
    private FormModel formModel;
    private String promptKey;
    
    public PromptTextFieldFormComponentInterceptor(){
        
    }

    public PromptTextFieldFormComponentInterceptor(FormModel formModel, MessageSource messageSource, String promptKey) {
        this.formModel = formModel;
        this.messageSource = messageSource;
        this.promptKey = promptKey;
    }

    @Override
    protected void processComponent(String propertyName, JTextComponent textComponent) {
        String prompt = messageSource.getMessage(new DefaultMessageSourceResolvable(getMessageKeys(formModel,propertyName), ""), Locale.getDefault());

        if (StringUtils.hasText(prompt)) {
            PromptSupport.setPrompt(prompt, textComponent);
        }
    }
    
    protected String[] getMessageKeys(FormModel formModel, String propertyName) {
        return new String[] { formModel.getId() + "." + propertyName + "." + promptKey,
                              propertyName + "." + promptKey };
    }

    
    
    
}
